package com.lwj;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableCircuitBreaker  //表示允许断路器
@EnableDiscoveryClient //发现客户端
@SpringBootApplication
public class RibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(RibbonApplication.class, args);
	}

	/*接着在启动类动一下手脚让我们配置的随机算法的负载均衡生效，只需要实现一个实现了IRule接口的Bean即可，如下：*/
	@Bean
	public IRule getIRule(){
		return new RandomRule();
	}

	/*接着在启动类加上服务发现注解，如下：
		@EnableDiscoveryClient
		启动类接着声明一个负载均衡的请求器@LoadBalanced，还有请求发起的工具RestTemplate*/

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}
}
